/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entitydao;

import database.Database;
import entity.Pujceno;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lattis
 */
public class PujcenoDAO extends TableDAO implements DAO<Pujceno> {

    public static final String TABLE_NAME = "LIB_PUJCENO";
    public static final String FIND_ALL = "SELECT * FROM LIB_PUJCENO";
    public static final String FIND_BY_NAVSTEVNIK = "SELECT * FROM LIB_PUJCENO WHERE ID_NAVSTEVNIK = ?";
    public static final String FIND_BY_KNIHA = "SELECT * FROM LIB_PUJCENO WHERE ID_KNIHA = ?";
    public static final String INSERT = "INSERT INTO LIB_PUJCENO VALUES(?,?)";
    public static final String DELETE = "DELETE FROM LIB_PUJCENO WHERE _ID_NAVSTEVNIK = ? AND ID_KNIHA = ?";
    Connection mConnection;
    benchmarkapplication.StopWatch sw = new benchmarkapplication.StopWatch();

    public PujcenoDAO(Database database) {
        super(database, TABLE_NAME);
        mConnection = database.Connection();
    }

    public List<Pujceno> findAll() {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Pujceno> pujcenoList = new ArrayList<Pujceno>();
        try {
            pstm = mConnection.prepareStatement(FIND_ALL);
            rs = pstm.executeQuery();
//            sw.start();
            while (rs.next()) {
                pujcenoList.add(new Pujceno(rs.getInt(1), rs.getInt(2)));
            }
//            sw.stop();
//            System.err.println(sw.getElapsedTimeSecs());
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            pujcenoList = null;
        } finally {
            close(rs, pstm);
        }
        return pujcenoList;
    }

    public List<Pujceno> findByNavstevnik(int id_navstevnik) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Pujceno> pujcenoList = new ArrayList<Pujceno>();
        try {
            pstm = mConnection.prepareStatement(FIND_BY_NAVSTEVNIK);
            pstm.setInt(1, id_navstevnik);
            rs = pstm.executeQuery();
            while (rs.next()) {
                pujcenoList.add(new Pujceno(rs.getInt(1), rs.getInt(2)));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            pujcenoList = null;
        } finally {
            close(rs, pstm);
        }
        return pujcenoList;
    }

    public List<Pujceno> findByKniha(int id_kniha) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Pujceno> pujcenoList = new ArrayList<Pujceno>();
        try {
            pstm = mConnection.prepareStatement(FIND_BY_KNIHA);
            pstm.setInt(1, id_kniha);
            rs = pstm.executeQuery();
            while (rs.next()) {
                pujcenoList.add(new Pujceno(rs.getInt(1), rs.getInt(2)));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            pujcenoList = null;
        } finally {
            close(rs, pstm);
        }
        return pujcenoList;
    }

    public Pujceno findById(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean insert(Pujceno entity) {
        PreparedStatement pstm = null;
        int rownum = 0;
        boolean ret = false;
        try {
            pstm = mConnection.prepareStatement(INSERT);
            pstm.setInt(1, entity.getNavstevnik());
            pstm.setInt(2, entity.getKniha());
            mDatabase.BeginTransaction();
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
            System.err.println(e.getMessage());
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
        }
        return ret;
    }

    public boolean delete(Pujceno entity) {
        PreparedStatement pstm = null;
        int rownum = 0;
        boolean ret = false;
        try {
            pstm = mConnection.prepareStatement(DELETE);
            pstm.setInt(1, entity.getNavstevnik());
            pstm.setInt(2, entity.getKniha());
            mDatabase.BeginTransaction();
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
//            System.err.println(e.getMessage());
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
        }
        return ret;
    }

    public boolean update(Pujceno entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void close(ResultSet rs, PreparedStatement pstm) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstm != null) {
                pstm.close();
            }
        } catch (Exception e) {
            System.err.println("Closing Error");
        }
    }
}
